﻿using System;
using System.ComponentModel;

namespace NanoMvvm.ComponentModel
{
    internal class StaticTypeDescriptionProvider : TypeDescriptionProvider
    {
        private static StaticTypeDescriptionProvider instance;

        private StaticTypeDescriptionProvider()
        {
        }
        
        internal static StaticTypeDescriptionProvider Instance
        {
            get
            {
                return instance ?? (instance = new StaticTypeDescriptionProvider());
            }
        }

        public static void Register(TypeViewModel tvm)
        {
            TypeDescriptor.AddProvider(Instance, tvm);
        }

        public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object objectInstance)
        {
            var tvm = objectInstance as TypeViewModel;
            if (tvm == null)
            {
                return new EmptyTypeDescriptor();
            }

            return StaticTypeDescriptor.GetDescriptor(tvm);
        }
    }
}